package org.coc.betterchicago;

import org.coc.betterchicago.beans.BetterChicagoUser;
import org.coc.betterchicago.beans.DAO;

import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.User;
import twitter4j.auth.AccessToken;

public class TwitterOAuth {

	public User authorize(long id) {
		try {
			DAO dao = new DAO();
			BetterChicagoUser user = dao.getBetterChicagoUser(id);
			if (user != null) {
				Twitter twitter = new TwitterFactory(BetterChicagoAuth.CONFIGURATION).getInstance();
				twitter.setOAuthAccessToken(new AccessToken(user.getToken(), user.getTokenSecret()));
				try {
					User twitterUser = twitter.verifyCredentials();
					return twitterUser;
				} catch (TwitterException e) {
					e.printStackTrace();
					return error();
				}
			}
			return error();
		} catch (Exception e) {
			e.printStackTrace();
			return error();
		}

	}

	private User error() {
		return null;
	}
}
